﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RpgMVX_SavesManager
{
    public partial class Settings : Form
    {
        private bool saved = false;
        private bool disposal = false;
        private SettingPack sp = new SettingPack();
        public Settings()
        {
            InitializeComponent();
        }

        private void saveSettings()
        {
            sp.namingPattern = this.nameRule.Text;
            if (!backupInGameFolder.Checked) sp.bFolder = backupPath.Text;
            if (sp.selfNaming) sp.namingPattern = Utils.isValidString(nameRule.Text) ? nameRule.Text : "[文件名]_[时间:年/月/日_时:分:秒:毫秒_时间截]_[编号].[后缀]_backup";
            saved = true;
            GetSetSettings.save(sp);
        }

        private void Settings_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (saved) return;
            if (disposal) return;
            DialogResult dr = MessageBox.Show("你还没保存，要保存吗？", "确定退出？", MessageBoxButtons.YesNoCancel);
            if (DialogResult.Yes == dr)
            {
                saveSettings();
                saved = true;
                this.Close();
            }
            else if (dr == DialogResult.Cancel)
            {
                e.Cancel = true;
            }
            else if (dr == DialogResult.No)
            {
                disposal = true;
                this.Close();
            }
        }

        private void save_Click(object sender, EventArgs e)
        {
            if(DialogResult.OK == MessageBox.Show("确定保存吗？", "保存更改", MessageBoxButtons.OKCancel))
            {
                saveSettings();
                this.Close();
            }
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("确定放弃更改吗？", "放弃更改", MessageBoxButtons.OKCancel))
            {
                disposal = true;
                this.Close();
            }
        }

        private void resetAll_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("你确定不是手滑点到重置了吗？", "重置设置", MessageBoxButtons.OKCancel))
            {
                disposal = true;
                GetSetSettings.reset();
                MainForm.reopenSettting();
            }
        }

        private void Settings_Load(object sender, EventArgs e)
        {
            SettingPack sp0 = new SettingPack();
            this.autoBackup.Checked = sp0.autoBackup;
            this.autoName.Checked = sp0.selfNaming;
            popupName.Checked = sp0.popup;
            nameByRule.Checked = !sp0.popup;
            backupInGameFolder.Checked = sp0.bGameFolder;
            nameRule.Text = sp0.namingPattern;
            backupPath.Text = sp0.bFolder;
        }

        private void popupName_CheckedChanged(object sender, EventArgs e)
        {
            this.nameByRule.Checked = !this.popupName.Checked;
            this.nameRule.Enabled = this.nameByRule.Checked;
            sp.popup = popupName.Checked;
            sp.selfNaming = this.nameByRule.Checked;
            sp.selfNaming = !popupName.Checked;
        }

        private void nameByRule_CheckedChanged(object sender, EventArgs e)
        {
            this.popupName.Checked = !this.nameByRule.Checked;
            this.nameRule.Enabled = this.nameByRule.Checked;
            sp.popup = this.popupName.Checked;
            sp.selfNaming = this.nameByRule.Checked;
            sp.namingPattern = this.nameRule.Text;
        }

        private void autoBackup_CheckedChanged(object sender, EventArgs e)
        {
            sp.autoBackup = this.autoBackup.Checked;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path;
            while ((path = Utils.openFolderSelect()) == "-1")
            {
                MessageBox.Show("请选择一个文件夹", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            this.backupPath.Text = path;
            sp.bFolder = path;
        }

        private void backupInGameFolder_CheckedChanged(object sender, EventArgs e)
        {
            button1.Enabled = !backupInGameFolder.Checked;
            sp.bGameFolder = backupInGameFolder.Checked;
        }

        private void autoName_CheckedChanged(object sender, EventArgs e)
        {
            sp.selfNaming = !autoName.Checked;
            popupName.Checked = popupName.Enabled = false;
            nameByRule.Checked = nameByRule.Enabled = false;
        }
    }
}
